package br.com.felix.fwt.ui.validation;

import java.util.ArrayList;
import java.util.List;

import br.com.felix.fwt.ui.BaseComponent;

public class FieldLengthValidator extends Validator{

	public FieldLengthValidator(BaseComponent... components) {
		super(components);
	}

	private int[] maxLengths;
	private int[] minLengths; 
	
	public void setMaxLengths(int ... maxLengths) {
		this.maxLengths = maxLengths;
	}
	
	public void setMinLengths(int ... minLengths) {
		this.minLengths = maxLengths;
	}
	
	@Override
	public List<ValidationError> isValid() {
		List<ValidationError> lst = new ArrayList<ValidationError>();
		BaseComponent[] components = getComponents();
		BaseComponent c;
		int maxLength;
		int minLength;
		for (int i=0; i< components.length; i++){
			c = components[i];
			String value = c.getValue();
			maxLength = maxLengths!=null?maxLengths[i]:0;
			minLength = minLengths!=null?minLengths[i]:0;
			if (value == null){
				continue;
			}
			if (value.length()>maxLength || value.length()<minLength){
				ValidationError ve = new ValidationError("FieldLengthError");
				ve.setReferences(c.getName(),minLength, maxLength);
				lst.add(ve);
			}
		}
		return lst;
	}

}
